﻿/// <reference path="../Turbo/Turbo_common.js" />

var ____a;

var $PropertyPair = TA.Turbo.Controls.PropertyPair = function PropertyPair(objectProperty, jsonProperty)
{
	this.ObjectProperty = objectProperty;
	this.JSONProperty = jsonProperty;
	this.Children = new Array();
}
if (____a)
{
	$PropertyPair.prototype.ObjectProperty = "";
	$PropertyPair.prototype.JSONProperty = "";
	$PropertyPair.prototype.Children = new Array();
}

var $RepeaterValue = TA.Turbo.Controls.RepeaterValue = function RepeaterValue()
{
	this.Entries = new Array();
	this._fields = new Array();
}
$RepeaterValue.Inherit(BaseJsonSerializable);
$RepeaterValue.Inherit(BaseJsonDeserializable);

if (____a)
{
	$RepeaterValue.prototype.Entries = new Array();
	$RepeaterValue.prototype._fields = new Array();
}
$RepeaterValue.prototype.NumEntries = 0;
$RepeaterValue.prototype.FirstIndex = 0;


$RepeaterValue.prototype.SetFields = function SetFields(json)
{
	this._SetFields_Recursive(this._fields, json);
}
$RepeaterValue.prototype._SetFields_Recursive = function _SetFields_Recursive(fields, json)
{
	for (var iField = 0; iField < json.length; ++iField)
	{
		var oneField = json[iField];

		var pp = new $PropertyPair(oneField[0], oneField[1]);
		if (oneField.length > 2)
		{
			this._SetFields_Recursive(pp.Children, oneField[2]);
		}
		fields.push(pp);
	}
}
$RepeaterValue.prototype.SerializeContents = function SerializeContents(jw)
{
	if (____a)
		jw = new JsonWriter();

	jw.PropertyName("E");
	jw.StartArray();
	for (var i = 0; i < this.Entries.length; ++i)
	{
		var oneEntry = this.Entries[i];
		this.SerializeOneEntry(jw, oneEntry);
	}
	jw.EndArray();

	jw.Property("N", this.NumEntries);
	jw.Property("F", this.FirstIndex);
}
$RepeaterValue.prototype.get_IsDirty = function get_IsDirty()
{
	return true;
}

$RepeaterValue.prototype.DeserializeContent = function DeserializeContent(prShortName, value)
{
	switch (prShortName)
	{
		case "E":
			this.Entries = new Array();
			for (var iEntry = 0; iEntry < value.length; ++iEntry)
			{
				var oneEntryJson = value[iEntry];
				this.Entries.push(this.DeserializeOneEntry(oneEntryJson));
			}
			break;
		case "N": this.NumEntries = value; break;
		case "F": this.FirstIndex = value; break;
	}
}
$RepeaterValue.prototype.SerializeOneEntry = function SerializeOneEntry(jw, oneEntry)
{
	this._SerializeOneEntry_Recursive(jw, oneEntry, this._fields);
}
$RepeaterValue.prototype._SerializeOneEntry_Recursive = function _SerializeOneEntry_Recursive(jw, data, fields)
{
	if (____a)
		jw = new JsonWriter();

	if (fields != null)
	{
		jw.StartObject();

		for (var iField = 0; iField < fields.length; ++iField)
		{
			var oneField = fields[iField];

			jw.PropertyName(oneField.JSONProperty);

			var value = data[oneField.ObjectProperty];
			if (oneField.Children.length == 0 || value == null)
			{
				jw.Value(value);
			}
			else if (value instanceof Array)
			{
				jw.StartArray();
				for (var iChild = 0; iChild < value.length; ++iChild)
				{
					var oneChild = value[iChild];

					this._SerializeOneEntry_Recursive(jw, oneChild, oneField.Children);
				}
				jw.EndArray();
			}
			else
			{
				this._SerializeOneEntry_Recursive(jw, data[oneField.ObjectProperty], oneField.Children);
			}
		}

		jw.EndObject();
	}
}
$RepeaterValue.prototype.DeserializeOneEntry = function DeserializeOneEntry(json)
{
	return this._DeserializeOneEntry_Recursive(json, this._fields);
}
$RepeaterValue.prototype._DeserializeOneEntry_Recursive = function _DeserializeOneEntry_Recursive(json, fields)
{
	if (fields != null)
	{
		var res = new Object();
		for (var iField = 0; iField < fields.length; ++iField)
		{
			var oneField = fields[iField];

			var jsonValue = json[oneField.JSONProperty];
			if (jsonValue == null || oneField.Children.length == 0)
			{
				res[oneField.ObjectProperty] = jsonValue;
			}
			else if (jsonValue instanceof Array)
			{
				var resArray = new Array();
				res[oneField.ObjectProperty] = resArray;

				for (var iChild = 0; iChild < jsonValue.length; ++iChild)
				{
					var jsonChild = jsonValue[iChild];

					resArray.push(this._DeserializeOneEntry_Recursive(jsonChild, oneField.Children));
				}
			}
			else
			{
				res[oneField.ObjectProperty] = this._DeserializeOneEntry_Recursive(json[oneField.JSONProperty], oneField.Children);
			}
		}

		return res;
	}
	return null;
}
